﻿//-----------------------------------------------------------------------
// <copyright file="ApplicationContext.cs" company="deixei.com">
//     Copyright (c) deixei.com. All rights reserved.
// </copyright>
// <author>Marcio Parente</author>
//-------------------------------------------------------------------
namespace Deixei.Web
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using Deixei;
    
    using Deixei.Domain;
    using Deixei.Domain.Repository;
    using Deixei.Domain.Service;
    using Deixei.Infrastructure;
    using Microsoft.Practices.Unity;
    using Microsoft.Practices.Unity.InterceptionExtension;

    public class ApplicationContext : Disposable
    {
        public ApplicationContext()
        {
            Initialize(null, null);
        }

        public ApplicationContext(ServiceLayerContext context)
        {
            Initialize(context, null);
        }

        public ApplicationContext(ServiceLayerContext context, IUnitOfWork unitOfWork)
        {
            Initialize(context, unitOfWork);
        }
        //Properties
        public IUnitOfWork UnitOfWork
        {
            get;
            private set;
        }
        public ServiceLayerContext ServiceLayerContext
        {
            get;
            private set;
        }
        //Methods
        private void Initialize(ServiceLayerContext context, IUnitOfWork unitOfWork)
        {
            this.UnitOfWork = ( unitOfWork ?? DeixeiContainer.Locator.GetInstance<UnitOfWork>() );
            this.ServiceLayerContext = ( context ?? DeixeiContainer.Locator.GetInstance<ServiceLayerContext>() );

            if (this.ServiceLayerContext.CustomerService == null)
            {
                this.ServiceLayerContext.CustomerService = DeixeiContainer.Locator.GetInstance<CustomerService>();
            }

            if ( this.ServiceLayerContext.NavigationService == null )
            {
                this.ServiceLayerContext.NavigationService = DeixeiContainer.Locator.GetInstance<NavigationService>();
            }

            if ( this.ServiceLayerContext.AuthorizationService == null )
            {
                this.ServiceLayerContext.AuthorizationService = DeixeiContainer.Locator.GetInstance<AuthorizationService>();
            }
        }

        protected override void DisposeCore()
        {
            UnitOfWork.Dispose();
            //base.Dispose();
        }

    }
}
